﻿#region

using System;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
using Class.Web.Mvc.Extensions;
using Class.Web.Mvc.Image;

#endregion

namespace Class.Web.Mvc
{
    public class BaseController : Controller
    {
        #region Resorce Manager

        protected string SaveResource<T>(string control, string alias, T t) where T : IImageContent
        {
            string result = "";

            HttpPostedFileBase file = Request.Files[control];
            if (file != null)
                if (file.ContentLength > 0)
                {
                    result = string.Format("{0}_{1}", t.GetImagePrefix(), file.FileName);
                    foreach (Size size in t.ImageSize)
                    {
                        if (size.Width == 0 || size.Height == 0)
                            file.SaveAs(string.Format("{0}{1}", Server.UploadPath(alias), result));
                        else
                        {
                            ProcessScaleImage.CreateThumbnail(file.InputStream, size, ScaleType.Center).Save(string.Format("{0}{1}x{2}_{3}", Server.UploadPath(alias), size.Width, size.Height, result));
                        }
                    }
                }
            return result;
        }

        protected string SaveBrushResource<T>(string control, string alias, T t) where T : IImageBrushContent
        {
            string result = "";

            HttpPostedFileBase file = Request.Files[control];
            if (file != null)
                if (file.ContentLength > 0)
                {
                    result = string.Format("{0}_{1}", t.GetImagePrefix(), file.FileName);
                    foreach (Size size in t.ImageBrushSize)
                    {
                        if (size.Width == 0 || size.Height == 0)
                            file.SaveAs(string.Format("{0}{1}", Server.UploadPath(alias), result));
                        else
                        {
                            ProcessScaleImage.CreateThumbnail(file.InputStream, size, ScaleType.Center, t.Brush).Save(string.Format("{0}{1}x{2}_{3}", Server.UploadPath(alias), size.Width, size.Height, result));
                        }
                    }
                }
            return result;
        }

        protected string SaveResource<T>(string control, string alias, string fileName, T t) where T : IImageContent
        {
            string result;

            HttpPostedFileBase file = Request.Files[control];
            if (file != null)
                if (file.ContentLength > 0)
                {
                    if (!string.IsNullOrEmpty(fileName))
                        DeleteResource(alias, fileName, t);

                    result = string.Format("{0}_{1}", t.GetImagePrefix(), file.FileName);
                    foreach (Size size in t.ImageSize)
                    {
                        if (size.Width == 0 || size.Height == 0)
                            file.SaveAs(string.Format("{0}{1}", Server.UploadPath(alias), result));
                        else
                            ProcessScaleImage.CreateThumbnail(file.InputStream, size, ScaleType.Center).Save(string.Format("{0}{1}x{2}_{3}", Server.UploadPath(alias), size.Width, size.Height, result));
                    }
                }
                else
                    result = fileName;
            else
                result = fileName;
            return result;
        }

        protected string SaveBrushResource<T>(string control, string alias, string fileName, T t) where T : IImageBrushContent
        {
            string result;

            HttpPostedFileBase file = Request.Files[control];
            if (file != null)
                if (file.ContentLength > 0)
                {
                    if (!string.IsNullOrEmpty(fileName))
                        DeleteBrushResource(alias, fileName, t);

                    result = string.Format("{0}_{1}", t.GetImagePrefix(), file.FileName);
                    foreach (Size size in t.ImageBrushSize)
                    {
                        if (size.Width == 0 || size.Height == 0)
                            file.SaveAs(string.Format("{0}{1}", Server.UploadPath(alias), result));
                        else
                            ProcessScaleImage.CreateThumbnail(file.InputStream, size, ScaleType.Center, t.Brush).Save(string.Format("{0}{1}x{2}_{3}", Server.UploadPath(alias), size.Width, size.Height, result));
                    }
                }
                else
                    result = fileName;
            else
                result = fileName;
            return result;
        }

        protected void DeleteResource<T>(string alias, string fileName, T t) where T : IImageContent
        {
            foreach (Size size in t.ImageSize)
            {
                if (size.Width == 0 || size.Height == 0)
                    Server.Delete(alias, fileName);
                else
                    Server.Delete(alias, string.Format("{0}x{1}_{2}", size.Width, size.Height, fileName));
            }
        }

        protected void DeleteBrushResource<T>(string alias, string fileName, T t) where T : IImageBrushContent
        {
            foreach (Size size in t.ImageBrushSize)
            {
                if (size.Width == 0 || size.Height == 0)
                    Server.Delete(alias, fileName);
                else
                    Server.Delete(alias, string.Format("{0}x{1}_{2}", size.Width, size.Height, fileName));
            }
        }

        #endregion

        public static string ConvertToUnSign(string text)
        {
            for (int i = 32; i < 48; i++)
                text = text.Replace(((char)i) + "", " ");
            text = text.Replace(".", "-");
            text = text.Replace(" ", "-");
            text = text.Replace(",", "-");
            text = text.Replace(";", "-");
            text = text.Replace(":", "-");

            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
            string strFormD = text.Normalize(NormalizationForm.FormD);
            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
        }

    }
}
